package com.itheima.controller;

import com.itheima.anno.Mylog;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Product;
import com.itheima.pojo.ProductQueryParam;
import com.itheima.pojo.Result;
import com.itheima.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService  productService;


    /**
     * 分页查询
     *  条件查询
     *  封装结果
     * @param productQueryParam
     * @return
     */
    //列表查询
    @GetMapping
    public Result page(ProductQueryParam productQueryParam){
        log.info("请求参数为:{}",productQueryParam);
        PageBean pageBean = productService.page(productQueryParam);
     return Result.success(pageBean);
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    //添加
    @Mylog
    @PostMapping
    public Result save(@RequestBody Product  product){
        log.info("请求参数为:{}",product);
        productService.save(product);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    //查询回显
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("要回显数据的id:{}",id);
        Product product = productService.getInfo(id);
        return Result.success(product);
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    //修改
    @Mylog
    @PutMapping
    public Result update(@RequestBody Product product){
        log.info("要修改的数据:{}",product);
        productService.update(product);
        return Result.success();
    }
    //删除
    @Mylog
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除商品ids:{}",ids);
        productService.deletByIds(ids);
        return Result.success();
    }
    //查询全部
    @GetMapping("/list")
    public Result list(){
       List<Product> productList= productService.queryProuctList();
       return Result.success(productList);
    }



}
